package com.woniu.idearbac.security;

import com.woniu.idearbac.entity.SysUser;
import com.woniu.idearbac.vo.PermissionVo;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 功能描述
 *
 * @author PC
 * @date -2022/12/1-时间-18:02
 * @product idea-rbac
 */
@Data
public class LoginUser implements UserDetails {

    private SysUser user;
    private List<PermissionVo> menuList;//得到当前用户的菜单
    private String token;
    private List<String> permissions;

    private List<String> roles;//得到当前用户的角色
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        ArrayList<SimpleGrantedAuthority> list = new ArrayList<>();
        if (permissions != null) {
            for (String permission : permissions) {
                list.add(new SimpleGrantedAuthority(permission));
            }
        }

        return list;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getTel();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
